<?php
class Jobmanager_LoginController extends Zend_Controller_Action
{
	public function loginAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$loginSession = new Zend_Session_Namespace('loginSession');
		if(isset($loginSession->loginFlg) && $loginSession->loginFlg != 1){
			echo $loginSession->user_id;
			return;
		}else{
			if ($this->getRequest()->isPost()) {
				$input = $this->getRequest()->getPost();
			}
			else {
				$input = $this->getRequest()->getQuery();
			}
			if(isset($input['username']) && isset($input['password'])){
				$userManager = new Model_UserManager();
				$username = $input['username'];
				$password = $input['password'];
				$user_id = $userManager->checkLogin($username, $password);
				if($user_id != null){// login success
					$userdetail = $userManager->getUserDetail($user_id);
					
					$loginSession->loginFlg = 1;
					$loginSession->user_id = $user_id;
					if($userdetail != null){
						$loginSession->user_name = $userdetail['user_name'];
						$loginSession->name = $userdetail['name'];
					}
					echo $loginSession->user_id;
				}else{
					unset($loginSession->loginFlg);
					echo -1;
				}
			}else{
				echo -2;
			}
		}
	}
	
	public function getloginuserinfoAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		if(isset($loginSession->loginFlg) && $loginSession->loginFlg != 1){
			$loginSession = new Zend_Session_Namespace('loginSession');
			$username = $loginSession->user_name;
			$name = $loginSession->name;
			$user_id = $loginSession->user_id;
			echo $user_id."-".$username."-".$name;
		}else{
			echo -1;
		}
	}
}